<?php

namespace app\admin\controller;

use clt\Tree;
use think\facade\Request;
use think\facade\Env;

class Recruit extends Common {

    protected $dao, $recruit, $module;

    function initialize() {
        parent::initialize();
        foreach ((array) $this->module as $rw) {
            if ($rw['type'] == 1 && $rw['status'] == 1) {
                $data['module'][$rw['id']] = $rw;
            }
        }
        $this->module = $data['module'];
        $this->assign($data);
        unset($data);
        $this->dao = db('recruit');
    }

    public function index() {
        if ($this->recruit) {
            foreach ($this->recruit as $r) {
                if ($r['module'] == 'page') {
                    $r['str_manage'] = '<a class="orange" href="' . url('page/edit', array('id' => $r['id'])) . '" title="修改内容"><i class="icon icon-file-text2"></i></a> | ';
                } else {
                    $r['str_manage'] = '';
                }
                $r['str_manage'] .= '<a class="green" href="' . url('recruit/edit', array('id' => $r['id'])) . '" title="修改"><i class="icon icon-pencil2"></i></a> | <a class="red" href="javascript:del(\'' . $r['id'] . '\')" title="删除"><i class="icon icon-bin"></i></a> ';

                $r['modulename'] = $this->module[$r['moduleid']]['title'];

                $array[] = $r;
            }

            $str = "<tr><td class='visible-lg visible-md'>\$id</td>";
            $str .= "<td class='text-left'>\$spacer<a href='/admin/\$module/recruit/\$files/\$id.html' class='green' title='查看内容'>\$catname </a>&nbsp;</td>";

            
            $str .= "<td>\$str_manage</td></tr>";
            $tree = new Tree($array);
            $tree->icon = array('&nbsp;&nbsp;&nbsp;│  ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
            $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
            $recruit = $tree->get_tree(0, $str);

            $this->assign('recruit', $recruit);
        }
        $this->assign('title', '岗位列表');
        return $this->fetch();
    }

    public function add() {
        $parentid = input('param.parentid');
        //模型列表
        $module = db('module')->where('status', 1)->field('id,title,name')->select();
        $this->assign('modulelist', $module);

        //父级模型ID
        $vo['moduleid'] = $this->recruit[$parentid]['moduleid'];
        $this->assign('module', $vo);

        $str = "<option value='\$id' \$selected>\$spacer \$catname</option>";
        $tree = new Tree($array);
        $recruit = $tree->get_tree(0, $str, $parentid);
        $this->assign('recruit', $recruit);
        //模版
        $templates = template_file();
        $this->assign('templates', $templates);
        //管理员权限组
        $usergroup = db('auth_group')->select();
        $this->assign('rlist', $usergroup);
        $this->assign('title', '添加岗位');
        return $this->fetch();
    }

    public function insert() {
        $data = $data = Request::except('file');
        if (!empty($data['readgroup'])) {
            $data['readgroup'] = implode(',', $data['readgroup']);
        }
        $data['moduleid'] = '13';
        $data['module'] = 'article';
        $id = db('recruit')->insertGetId($data);
        if ($id) {
            if ($data['module'] == 'page') {
                $data2['id'] = $id;
                if ($data['title'] == '') {
                    $data2['title'] = $data['catname'];
                }

                $page = db('page');
                $page->insert($data2);
            }
            $this->repair();
            $this->repair();
            savecache('recruit');
            $result['msg'] = '分类添加成功!';
            cache('cate', NULL);
            $result['url'] = url('index');
            $result['code'] = 1;
            return $result;
        } else {
            $result['msg'] = '分类添加失败!';
            $result['code'] = 0;
            return $result;
        }
    }

    public function edit() {
        $id = input('id');
        $this->assign('module', $this->recruit[$id]['moduleid']);
        $module = db('module')->field('id,title,name')->select();
        $this->assign('modulelist', $module);

        $record = $this->recruit[$id];
        $record['imgUrl'] = imgUrl($record['image']);
        $record['readgroup'] = explode(',', $record['readgroup']);
        $parentid = intval($record['parentid']);
        $result = $this->recruit;
        foreach ($result as $r) {
            if ($r['type'] == 1)
                continue;
            $r['selected'] = $r['id'] == $parentid ? 'selected' : '';
            $array[] = $r;
        }
        $str = "<option value='\$id' \$selected>\$spacer \$catname</option>";
        $tree = new Tree($array);
        $recruit = $tree->get_tree(0, $str, $parentid);
        $this->assign('recruit', $recruit);
        $this->assign('record', $record);
        $usergroup = db('auth_group')->select();
        $this->assign('rlist', $usergroup);
        $this->assign('title', '编辑分类');
        //模版
        $templates = template_file();
        $this->assign('templates', $templates);
        return $this->fetch();
    }

    public function catUpdate() {
        $data = $data = Request::except('file');
//        $data['module'] = db('module')->where(array('id' => $data['moduleid']))->value('name');
//        if (!empty($data['readgroup'])) {
//            $data['readgroup'] = implode(',', $_POST['readgroup']);
//        } else {
//            $data['readgroup'] = '';
//        }
//        $data['arrparentid'] = $this->get_arrparentid($data['id']);
//        $data['child'] = ($data['child'] == 1) ? '1' : '0';
        if (false !== db('recruit')->update($data)) {
//            if ($data['child'] == 1) {
//                $arrchildid = $this->get_arrchildid($data['id']);
//                $data2['ismenu'] = $data['ismenu'];
//                $data2['pagesize'] = $data['pagesize'];
//                $data2['readgroup'] = $data['readgroup'];
//                db('recruit')->where(' id in (' . $arrchildid . ')')->update($data2);
//            }
            $this->repair();
            savecache('recruit');
            $result['msg'] = '分类修改成功!';
            cache('cate', NULL);
            $result['url'] = url('index');
            $result['code'] = 1;
            return $result;
        } else {
            $result['msg'] = '分类修改失败!';
            $result['code'] = 0;
            return $result;
        }
    }

    public function repair() {
        @set_time_limit(500);
        $this->recruit = $recruit = array();
        $recruit = db('recruit')->where("parentid=0")->order('listorder ASC,id ASC')->select();
        $this->set_recruit($recruit);
        if (is_array($this->recruit)) {
            foreach ($this->recruit as $id => $cat) {
                if ($id == 0 || $cat['type'] == 1)
                    continue;
                $this->recruit[$id]['arrparentid'] = $arrparentid = $this->get_arrparentid($id);
                $this->recruit[$id]['arrchildid'] = $arrchildid = $this->get_arrchildid($id);
                $this->recruit[$id]['parentdir'] = $parentdir = $this->get_parentdir($id);
                db('recruit')->update(array('parentdir' => $parentdir, 'arrparentid' => $arrparentid, 'arrchildid' => $arrchildid, 'id' => $id));
            }
        }
    }

    public function set_recruit($recruit = array()) {
        if (is_array($recruit) && !empty($recruit)) {
            foreach ($recruit as $id => $c) {
                $this->recruit[$c['id']] = $c;
                $r = db('recruit')->where(array("parentid" => $c['id']))->Order('listorder ASC,id ASC')->select();
                $this->set_recruit($r);
            }
        }
        return true;
    }

    public function get_arrparentid($id, $arrparentid = '') {
        if (!is_array($this->recruit) || !isset($this->recruit[$id]))
            return false;
        $parentid = $this->recruit[$id]['parentid'];
        $arrparentid = $arrparentid ? $parentid . ',' . $arrparentid : $parentid;
        if ($parentid) {
            $arrparentid = $this->get_arrparentid($parentid, $arrparentid);
        } else {
            $this->recruit[$id]['arrparentid'] = $arrparentid;
        }
        return $arrparentid;
    }

    public function get_arrchildid($id) {
        $arrchildid = $id;
        if (is_array($this->recruit)) {
            foreach ($this->recruit as $catid => $cat) {
                if ($cat['parentid'] && $id != $catid) {
                    $arrparentids = explode(',', $cat['arrparentid']);
                    if (in_array($id, $arrparentids)) {
                        $arrchildid .= ',' . $catid;
                    }
                }
            }
        }
        return $arrchildid;
    }

    public function get_parentdir($id) {
        if ($this->recruit[$id]['parentid'] == 0) {
            return '';
        }
        $arrparentid = $this->recruit[$id]['arrparentid'];
        unset($r);
        if ($arrparentid) {
            $arrparentid = explode(',', $arrparentid);
            $arrcatdir = array();
            foreach ($arrparentid as $pid) {
                if ($pid == 0)
                    continue;
                $arrcatdir[] = $this->recruit[$pid]['catdir'];
            }
            return implode('/', $arrcatdir) . '/';
        }
    }

    public function del() {
        $catid = input('param.id');
        $modules = $this->recruit[$catid]['module'];
        $modulesId = $this->recruit[$catid]['moduleid'];
        $scount = $this->dao->where(array('parentid' => $catid))->count();

        if ($scount) {
            $result['info'] = '请先删除其子分类!';
            $result['status'] = 0;
            return $result;
        }
        $module = db('recruitarticle');
        $arrchildid = $this->recruit[$catid]['arrchildid'];

        if ($modules != 'page') {
            $fields = F($modulesId . '_Field');
            $fieldse = array();
            foreach ($fields as $k => $v) {
                $fieldse[] = $k;
            }
            if (in_array('catid', $fieldse)) {
                $count = $module->where('catid', 'in', $arrchildid)->count();
            } else {
                $count = $module->count();
            }
            if ($count) {
                $result['info'] = '请先删除该分类下所有数据!';
                $result['status'] = 0;
                return $result;
            }
        }
        $pid = $this->recruit[$catid]['parentid'];

        $scat = $this->dao->where(array('parentid' => $pid))->count();
        if ($scat == 1) {
            db('recruit')->where(array('id' => $pid))->update(array('child' => 0));
        }
        db('recruit')->where('id', 'in', $arrchildid)->delete();
        $arr = explode(',', $arrchildid);
        foreach ((array) $arr as $r) {
            if ($this->recruit[$r]['module'] == 'page') {
                $module = db('page');
                $module->delete($r);
            }
        }
        $this->repair();
        savecache('recruit');
        $result['info'] = '分类删除成功!';
        cache('cate', NULL);
        $result['url'] = url('index');
        $result['status'] = 1;
        return $result;
    }

    public function cOrder() {
        $data = input('post.');
        $this->dao->update($data);
        $result = ['msg' => '排序成功！', 'code' => 1, 'url' => url('index')];
        savecache('recruit');
        cache('cate', NULL);
        return $result;
    }

}
